﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Engine.MarchCube
{
    public enum MType
    {
        Custom = 0,
        Sphere = 1,
        Diamond = 2,
        cube = 3,
        TT=4
        
    }
    public struct Melement
    {
        float x, y, z, radius, inner_radius;
        double baseF;
        byte type;
        Color rgb;
        BaseElement custData;
        public BaseElement GetData
        {
            get { return custData; }
            set { custData = value; }
        }
        string fData;
        public string File
        {
            get { return fData; }
            set { fData = value; }
        }
        public Melement(float X, float Y, float Z, float R, byte type)
        {
            // Set type/x/y/radius, and initialize to random velocity
            this.type = type;
            x = X;
            y = Y;
            z = Z;
            baseF = (R / ((X) * (X) + (Y) * (Y) + (Z) * (Z) + 1)) / 255d;
            radius = R;
            inner_radius = radius / 2;
            Random rnd = new Random(Environment.TickCount);
            rgb = new Color((byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255));
            custData = new BaseElement(1, Vector3.Zero, ""); 
            fData = "";
            
        }
        public Melement(float X, float Y, float Z, int size, string fileData )
        {
            // Set type/x/y/radius, and initialize to random velocity
            this.type = 0;
            x = X;
            y = Y;
            z = Z;
            baseF = (1 / ((X) * (X) + (Y) * (Y) + (Z) * (Z) + 1)) / 255d;
            radius = 1;
            inner_radius = radius / 2;
            Random rnd = new Random(Environment.TickCount);
            rgb = new Color((byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255));
            custData = new BaseElement(size,new Vector3(X,Y,Z), fileData);
            fData = fileData;
           
           
            
        }
        public  void loadData()
        {
            custData.load(fData);
          //  custData.RndDat();
           // custData.Save() ;
        }
        public void SaveData()
        {
            custData.Save(fData);
        }
        public float function(float X, float Y, float Z)
        {
            float tmp = 0;
            switch ((MType)type)
            {
                case MType.Sphere:
                    tmp = radius/((x - X) * (x - X) + (y - Y) * (y - Y) + (z - Z) * (z - Z));
                    break;
                case MType.Diamond:
                    tmp=(radius) / ((Math.Abs(X - x) + Math.Abs(Y - y) + Math.Abs(Z - z)));
                    break;
               case MType.cube:
                    tmp = (float)((radius) / (Math.Pow(x - X, 100d) + Math.Pow(y - Y, 100d) + Math.Pow(z - Z, 100d)));
                    break;
               case MType.TT:
                    tmp = (float)Math.Cos(X - x) + (float)Math.Cos(Y - y) + (float)Math.Cos(Z - z);
                    break;
               case MType.Custom:
                   OutResult res = custData.chekPos(X,Y,Z);
                 //  if (!res.isNull)
                  // {
                       tmp = res.Iso;
                //   }
                 //  else
                 //  {
                 //      tmp = 0;
                 //  }
                    break;   
                default:
                    break;
            }
            return tmp;
        }
        public Color Collor(double X, double Y, double Z)
        {
            double tmp = 0;
            Color r = Color.Transparent;
            switch ((MType)type)
            {
                case MType.Sphere:
                    tmp = radius / ((x - X) * (x - X) + (y - Y) * (y - Y) + (z - Z) * (z - Z));
                    break;
                case MType.Custom:
                    r = custData.GetColor(X,Y,Z);
                    break;
                default:
                    break;
            }
            if (r==Color.Transparent)
            {
                return new Color((byte)(tmp / baseF), (byte)(tmp / baseF), (byte)(tmp / baseF));
            }
            return r;
        }
    }
}
